home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / misc / confdb.c < prev    next >
C/C++ Source or Header  |  1995-11-10  |  5KB  |  262 lines

  1. #pragma implementation
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "misc.h"
  5. #include "confdb.h"
  6.  
  7. PUBLIC CONFOBJ::CONFOBJ (
  8.     const char *_key,
  9.     const char *_val)
  10. {
  11.     key.setfrom (_key);
  12.     val.setfrom (_val);
  13. }
  14.  
  15.  
  16. PUBLIC CONFDB::CONFDB(CONFIG_FILE &_fcfg)
  17.     : fcfg(_fcfg)
  18. {
  19.     /* #Specification: misc / CONFDB / intro
  20.         The CONFDB object was designed to support the
  21.         /etc/conf.linuxconf file. This save configuration
  22.         information in an ascii file with a simple one line
  23.         one record format.
  24.  
  25.         The line start with a key and the value(s) (words  or whatever)
  26.         follow up to the end of the line. The key is separated
  27.         from the value(s) by blank characters.
  28.     */
  29.     FILE *fin = fcfg.fopen ("r");
  30.     if (fin != NULL){
  31.         char buf[500];
  32.         int noline = 0;
  33.         while (fgets(buf,sizeof(buf),fin)!=NULL){
  34.             /* #Specification: /etc/conf.linuxconf / format
  35.                 /etc/conf.linuxconf is an ascii file. It
  36.                 contain all the information used to configured
  37.                 most services which lack a standard configuration
  38.                 file. Its format is simple
  39.  
  40.                 #
  41.                 keyword value ...
  42.                 #
  43.  
  44.                 The file is maintain by linuxconf.
  45.                 No comments or whatever are allowed.
  46.  
  47.                 This mecanism is handled by the CONFDB
  48.                 object. It is expect to be used for
  49.                 other stuff than /etc/conf.linuxconf.
  50.             */
  51.             noline++;
  52.             strip_end (buf);
  53.             if (buf[0] != '\0'){
  54.                 char keyw[100];
  55.                 char *pt = str_copyword (keyw,buf);
  56.                 pt = str_skip(pt);
  57.                 ARRAY::add (new CONFOBJ(keyw,pt));
  58.             }
  59.         }
  60.         fclose (fin);
  61.     }
  62. }
  63.  
  64. PUBLIC CONFOBJ *CONFDB::getitem(int no)
  65. {
  66.     return (CONFOBJ*)ARRAY::getitem(no);
  67. }
  68.  
  69. /*
  70.     Update the configuration file
  71.     Return -1 if any error.
  72. */
  73. PUBLIC int CONFDB::save()
  74. {
  75.     int ret = -1;
  76.     FILE *fout = fcfg.fopen ("w");
  77.     if (fout != NULL){
  78.         int nb = getnb();
  79.         for (int i=0; i<nb; i++){
  80.             CONFOBJ *o = getitem(i);
  81.             fprintf (fout,"%s %s\n",o->key.get()
  82.                 ,o->val.get());
  83.         }
  84.         ret = fclose (fout);
  85.     }
  86.     return ret;
  87. }
  88.  
  89. /*
  90.     Build a key
  91. */
  92. static const char *confdb_bkey(const char *prefix, const char *key)
  93. {
  94.     /* #Specification: linuxconf / /etc/conf.linuxconf / keys
  95.         The keyword of /etc/conf.linuxconf use a special
  96.         format convention. With a dot notation, the keyword
  97.         is splitted in two part: The first identify the
  98.         system and the second represent one parameter of this
  99.         system. This strategy prevent clashes between two
  100.         systems.
  101.     */
  102.     static char buf[100];
  103.     strcpy (buf,prefix);
  104.     strcat (buf,".");
  105.     strcat (buf,key);
  106.     return buf;
  107. }
  108.  
  109. /*
  110.     Find a record.
  111.     Return NULL if not found.
  112. */
  113. PUBLIC const char *CONFDB::getval(
  114.     const char *prefix,
  115.     const char *key,
  116.     const char *defval)
  117. {
  118.     int nb = getnb();
  119.     const char *bkey = confdb_bkey(prefix,key);
  120.     for (int i=0; i<nb; i++){
  121.         CONFOBJ *o = getitem(i);
  122.         if (o->key.cmp(bkey)==0){
  123.             defval = o->val.get();
  124.             break;
  125.         }
  126.     }
  127.     return defval;
  128. }
  129. /*
  130.     Find a record.
  131.     Return NULL if not found.
  132. */
  133. PUBLIC const char *CONFDB::getval(const char *prefix, const char *key)
  134. {
  135.     return getval (prefix,key,NULL);
  136. }
  137. /*
  138.     Locate one numeric configuration parameter.
  139.     Return defval if not found.
  140. */
  141. PUBLIC int CONFDB::getvalnum (
  142.     const char *prefix,
  143.     const char *key,
  144.     int defval)
  145. {
  146.     const char *val = getval (prefix,key);
  147.     if (val != NULL) defval = atoi(val);
  148.     return defval;
  149. }
  150.  
  151. /*
  152.     Locate all configuration parameter with the same key.
  153.     Return the number found.
  154. */
  155. PUBLIC int CONFDB::getall (
  156.     const char *prefix,
  157.     const char *key,
  158.     SSTRINGS &lst,
  159.     int copy)    // Take a copy of the values
  160. {
  161.     int ret = 0;
  162.     if (!copy) lst.neverdelete();
  163.     int nb = getnb();
  164.     const char *bkey = confdb_bkey(prefix,key);
  165.     for (int i=0; i<nb; i++){
  166.         CONFOBJ *o = getitem(i);
  167.         if (o->key.cmp(bkey)==0){
  168.             SSTRING *val = &o->val;
  169.             if (copy) val = new SSTRING (*val);
  170.             lst.add (val);
  171.             ret++;
  172.         }
  173.     }
  174.     return ret;
  175. }
  176.  
  177.  
  178. /*
  179.     Remove all entry with a given key.
  180. */
  181. PUBLIC void CONFDB::removeall (const char *prefix, const char *key)
  182. {
  183.     const char *bkey = confdb_bkey(prefix,key);
  184.     int nb = getnb();
  185.     for (int i=0; i<nb; i++){
  186.         CONFOBJ *o = getitem(i);
  187.         if (o->key.cmp(bkey)==0){
  188.             remove_del (o);
  189.             i--;
  190.             nb--;
  191.         }
  192.     }
  193. }
  194.  
  195.  
  196.  
  197.  
  198. /*
  199.     Add one record to the configuration file
  200. */
  201. PUBLIC void CONFDB::add (
  202.     const char *prefix,
  203.     const char *key,
  204.     const char *val)
  205. {
  206.     const char *bkey = confdb_bkey(prefix,key);
  207.     ARRAY::add (new CONFOBJ(bkey,val));
  208. }
  209.  
  210. /*
  211.     Add one record to the configuration file
  212. */
  213. PUBLIC void CONFDB::add (
  214.     const char *prefix,
  215.     const char *key,
  216.     const SSTRING &val)
  217. {
  218.     add (prefix,key,val.get());
  219. }
  220.  
  221. /*
  222.     Replace one record in the configuration file
  223. */
  224. PUBLIC void CONFDB::replace (const char *prefix, const char *key, const char *val)
  225. {
  226.     removeall(prefix,key);
  227.     add (prefix,key,val);
  228. }
  229.  
  230. /*
  231.     Replace one record in the configuration file
  232. */
  233. PUBLIC void CONFDB::replace (const char *prefix, const char *key, int val)
  234. {
  235.     char buf[20];
  236.     sprintf (buf,"%d",val);
  237.     replace (prefix,key,buf);
  238. }
  239. /*
  240.     Replace one record in the configuration file
  241. */
  242. PUBLIC void CONFDB::replace (
  243.     const char *prefix,
  244.     const char *key,
  245.     const SSTRING &val)
  246. {
  247.     replace (prefix,key,val.get());
  248. }
  249.  
  250. /*
  251.     Replace one record in the configuration file
  252. */
  253. PUBLIC void CONFDB::replace (
  254.     const char *prefix,
  255.     const char *key,
  256.     const SSTRINGS &vals)
  257. {
  258.     removeall(prefix,key);
  259.     int nb = vals.getnb();
  260.     for (int i=0; i<nb; i++) add (prefix,key,*vals.getitem(i));
  261. }
  262.